package com.party.mem.web.controller.file;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.party.common.paging.Page;
import com.party.common.utils.StringUtils;
import com.party.core.model.YesNoStatus;
import com.party.core.model.file.VideoResource;
import com.party.core.service.file.IVideoResourceService;
import com.party.mem.utils.RealmUtils;
import com.party.mem.web.dto.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.LinkedList;
import java.util.List;

/**
 * 图片资源
 */
@Controller
@RequestMapping(value = "/file/video")
public class VideoResourceController {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private IVideoResourceService videoResourceService;

    /**
     * 打开选择视频
     *
     * @param min      最小选择数
     * @param max      最大选择数
     * @param type     业务类型
     * @param backType 返回的类型 iframe或video
     * @return
     */
    @RequestMapping(value = "/selVideo")
    public ModelAndView selVideo(Integer min, Integer max, String type, String backType) {
        ModelAndView mv = new ModelAndView("resource/selVideo");
        mv.addObject("min", min);
        mv.addObject("max", max);
        mv.addObject("type", type);
        mv.addObject("backType", backType);
        return mv;
    }

    /**
     * 保存
     *
     * @param imgs
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/api/save")
    public AjaxResult save(String imgs) {
        try {
            List<VideoResource> imgResources = JSONObject.parseArray(imgs, VideoResource.class);
            LinkedList<String> ids = new LinkedList<String>();
            for (VideoResource img : imgResources) {
                String id = videoResourceService.insert(img);
                ids.add(id);
            }
            return AjaxResult.success(imgResources);
        } catch (Exception e) {
            logger.error("保存视频资源异常", e);
            return AjaxResult.error("保存异常");
        }
    }

    @ResponseBody
    @RequestMapping(value = "api/updatePoster")
    public AjaxResult updatePosterUrl(String videos) {
        try {
            List<VideoResource> imgResources = JSONObject.parseArray(videos, VideoResource.class);
            for (VideoResource img : imgResources) {
                videoResourceService.update(img);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            logger.error("保存视频资源异常", e);
            return AjaxResult.error("保存异常");
        }
    }

    /**
     * 读取列表
     *
     * @param img
     * @param pageNo
     * @param limit
     * @return
     */
    @ResponseBody
    @RequestMapping("/api/list")
    public AjaxResult list(VideoResource img, Integer pageNo, Integer limit) {
        try {
            Page page = new Page();
            page.setLimit(limit);
            page.setPage(pageNo);
            img.setDelFlag(YesNoStatus.NO.getCode().toString());
            List<VideoResource> imgs = videoResourceService.listPage(img, page);
            if (null == imgs) {
                imgs = Lists.newArrayList();
            }
            return AjaxResult.success(imgs, page);
        } catch (Exception e) {
            logger.error("获取视频列表异常", e);
            return AjaxResult.error("获取列表异常");
        }
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/api/del")
    public AjaxResult del(String id) {
        if (StringUtils.isEmpty(id)) {
            return AjaxResult.error("id不能为空");
        }
        try {
            if (StringUtils.isNotEmpty(id)) {
                videoResourceService.delete(id);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            logger.error("删除失败", e);
            return AjaxResult.error("删除失败");
        }
    }

    @RequestMapping(value = "/iframePage")
    public ModelAndView preview(String id) {
        ModelAndView mv = new ModelAndView("resource/videoIframe");
        VideoResource videoResource = videoResourceService.get(id);
        mv.addObject("videoResource", videoResource);
        return mv;
    }

    @ResponseBody
    @RequestMapping(value = "getVideoResource")
    public AjaxResult getVideoResource(String name, Long size) {
        try {
            if (StringUtils.isNotEmpty(name) && null != size) {
                VideoResource videoResource = new VideoResource();
                videoResource.setName(name);
                videoResource.setSize(size);
                videoResource.setCreateBy(RealmUtils.getCurrentUser().getId());
                VideoResource db = videoResourceService.getUnique(videoResource);
                if (null != db) {
                    // 存在
                    return AjaxResult.success(true);
                }
            }
        } catch (Exception e) {
            logger.error("获取视频是否存在异常", e);
        }
        return AjaxResult.success(false);
    }
}
